<html>
<head><meta charset="utf-8"><title>atomic intrinsic design · t-lang/wg-unsafe-code-guidelines · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/index.html">t-lang/wg-unsafe-code-guidelines</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/atomic.20intrinsic.20design.html">atomic intrinsic design</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="171789348"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/atomic%20intrinsic%20design/near/171789348" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/atomic.20intrinsic.20design.html#171789348">(Jul 26 2019 at 16:55)</a>:</h4>
<p><span class="user-mention" data-user-id="132920">@gnzlbg</span> </p>
<blockquote>
<p>FWIW I never understood why we need <code>AtomicXY</code> types, instead of having <code>ptr.atomic_load(...)</code> methods.</p>
</blockquote>
<p>Yeah I wondered the same. It's very annoying that e.g. <code>AtomicCell</code> has to transmute <code>(u8, u16)</code> to <code>u32</code>  to implement atomic accesses...</p>



<a name="171789499"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/atomic%20intrinsic%20design/near/171789499" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/atomic.20intrinsic.20design.html#171789499">(Jul 26 2019 at 16:56)</a>:</h4>
<p>I think it would be better to have intrinsics taking pointers that can be used to build the types.</p>



<a name="171789733"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/atomic%20intrinsic%20design/near/171789733" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/atomic.20intrinsic.20design.html#171789733">(Jul 26 2019 at 16:58)</a>:</h4>
<p><span aria-label="+1" class="emoji emoji-1f44d" role="img" title="+1">:+1:</span></p>



<a name="171833981"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/atomic%20intrinsic%20design/near/171833981" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/atomic.20intrinsic.20design.html#171833981">(Jul 27 2019 at 07:26)</a>:</h4>
<p>I think it was <em>an attempt</em> to try and limit the types that you'd use atomic with, in the same way that right now you can volatile load and store any pointer but it only works without tearing at specific sizes, per arch</p>



<a name="171833994"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/atomic%20intrinsic%20design/near/171833994" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/atomic.20intrinsic.20design.html#171833994">(Jul 27 2019 at 07:27)</a>:</h4>
<p>Also, it makes statics way easier</p>



<a name="171834335"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/atomic%20intrinsic%20design/near/171834335" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/atomic.20intrinsic.20design.html#171834335">(Jul 27 2019 at 07:37)</a>:</h4>
<p>what is the interaction with statics?</p>



<a name="171836815"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/atomic%20intrinsic%20design/near/171836815" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/atomic.20intrinsic.20design.html#171836815">(Jul 27 2019 at 08:56)</a>:</h4>
<p>so, when you have a static value you declare it</p>
<div class="codehilite"><pre><span></span><span class="k">pub</span><span class="w"> </span><span class="k">static</span><span class="w"> </span><span class="n">SDL_ACTIVE</span>: <span class="nc">AtomicBool</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">AtomicBool</span>::<span class="n">new</span><span class="p">(</span><span class="kc">false</span><span class="p">);</span><span class="w"></span>
</pre></div>


<p>or similar. Then every thread sees it and can safely call load and store all that. If there wasn't a dedicated type for this you'd have to do a whole song and dance to make an atomically editable bool. It'd be a <code>static mut</code>, and then you'd pray to Ferris and unsafely take a <code>&amp;mut</code> to it in the hope that no other thread was doing that right then, call <code>atomic_whatever</code>, and it'd just be a lot more error prone.</p>



<a name="171837028"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/atomic%20intrinsic%20design/near/171837028" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/atomic.20intrinsic.20design.html#171837028">(Jul 27 2019 at 09:04)</a>:</h4>
<p><span class="user-mention" data-user-id="224471">@Lokathor</span> we are only talking about the internal intrinsics here that are used to implement these types; I agree that <code>AtomicBool</code> makes sense as a user-facing type.</p>



<a name="171837039"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/atomic%20intrinsic%20design/near/171837039" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/atomic.20intrinsic.20design.html#171837039">(Jul 27 2019 at 09:05)</a>:</h4>
<p>however, it should be possible to do atomic accesess on a <code>*mut (u8, u16)</code>. currently you have to cast to <code>*mut u32</code> which is (a) hacky and (b) raises concerns about padding.</p>



<a name="171837300"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/atomic%20intrinsic%20design/near/171837300" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/atomic.20intrinsic.20design.html#171837300">(Jul 27 2019 at 09:15)</a>:</h4>
<p>I don't understand how <code>atomic_whatever::&lt;(u8, u16)&gt;(ptr, ...)</code> would help with anything (it actually exists internally!). The hardware only provides atomic accesses to (naturally aligned) 8/16/32/... bit chunks. If you want to use the 32 bit atomics with a honest-to-god <code>(u8, u16)</code>, you have to worry about the padding (and alignment) anyway at some level because it's a fundamental mismatch.</p>



<a name="171837478"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/atomic%20intrinsic%20design/near/171837478" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/atomic.20intrinsic.20design.html#171837478">(Jul 27 2019 at 09:21)</a>:</h4>
<p>ah! for internals, okay</p>



<a name="171838501"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/atomic%20intrinsic%20design/near/171838501" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/atomic.20intrinsic.20design.html#171838501">(Jul 27 2019 at 09:54)</a>:</h4>
<p><span class="user-mention" data-user-id="124289">@rkruppe</span> at least for loads and stores, there's no good reason I should have to transmute my <code>(u8, u16)</code> to <code>u32</code> before storing it</p>



<a name="171838773"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/atomic%20intrinsic%20design/near/171838773" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/atomic.20intrinsic.20design.html#171838773">(Jul 27 2019 at 10:03)</a>:</h4>
<p>Fair, though there's still alignment concerns -- you can't load/store with arbitrary <code>&amp;mut (u8, u16)</code>, it has to be aligned to 4 bytes. In fact, I recall the existing unstable intrinsics could be instantiated with <code>(u8, u16)</code> but rustc would put the wrong alignment on the LLVM intrinsic.</p>



<a name="171839185"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/atomic%20intrinsic%20design/near/171839185" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Славик Бут <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/atomic.20intrinsic.20design.html#171839185">(Jul 27 2019 at 10:17)</a>:</h4>
<p>(deleted)</p>



<a name="171839422"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/atomic%20intrinsic%20design/near/171839422" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/atomic.20intrinsic.20design.html#171839422">(Jul 27 2019 at 10:24)</a>:</h4>
<p>hm, yeah, I didnt think about alignment for a sec and there it got me again</p>



<a name="171842227"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/atomic%20intrinsic%20design/near/171842227" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/atomic.20intrinsic.20design.html#171842227">(Jul 27 2019 at 11:46)</a>:</h4>
<p><code>atomic_relaxed_load_u32(ptr: *const u32) -&gt; MaybeUninit&lt;u32&gt;</code> would just require <code>ptr</code> to be aligned to a 4-byte boundary</p>



<a name="171842236"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/atomic%20intrinsic%20design/near/171842236" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/atomic.20intrinsic.20design.html#171842236">(Jul 27 2019 at 11:47)</a>:</h4>
<p>So if you have:</p>
<div class="codehilite"><pre><span></span><span class="kd">let</span><span class="w"> </span><span class="n">x</span>: <span class="p">(</span><span class="kt">u8</span><span class="p">,</span><span class="w"> </span><span class="kt">u16</span><span class="p">);</span><span class="w"></span>
<span class="kd">let</span><span class="w"> </span><span class="n">y</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">unsafe</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">atomic_relaxed_load_u32</span><span class="p">(</span><span class="o">&amp;</span><span class="n">x</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="o">*</span><span class="k">const</span><span class="w"> </span><span class="n">_</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="o">*</span><span class="k">const</span><span class="w"> </span><span class="n">_</span><span class="p">)</span><span class="w"></span>
<span class="p">};</span><span class="w"></span>
</pre></div>


<p>and if the pointer isn't properly aligned the behavior is undefined. <code>clippy</code> will actually yell at this code, warning that you are casting pointers with different alignment requirements.</p>



<a name="171842291"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/atomic%20intrinsic%20design/near/171842291" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/atomic.20intrinsic.20design.html#171842291">(Jul 27 2019 at 11:49)</a>:</h4>
<p>I don't see how alignment is any more problematic than, e.g., size, with such an API. For example:</p>
<div class="codehilite"><pre><span></span><span class="kd">let</span><span class="w"> </span><span class="n">x</span>: <span class="kt">u8</span><span class="p">;</span><span class="w"></span>
<span class="kd">let</span><span class="w"> </span><span class="n">y</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">unsafe</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">atomic_relaxed_load_u32</span><span class="p">(</span><span class="o">&amp;</span><span class="n">x</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="o">*</span><span class="k">const</span><span class="w"> </span><span class="n">_</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="o">*</span><span class="k">const</span><span class="w"> </span><span class="n">_</span><span class="p">)</span><span class="w"></span>
<span class="p">};</span><span class="w"></span>
</pre></div>


<p>would read out-of-bounds, which is also UB.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>